package com.yjhb.android.sbz.v2.fragment.reward

import android.view.View
import com.alibaba.fastjson.JSONArray
import com.alibaba.fastjson.JSONObject
import com.yjhb.android.sbz.R
import com.yjhb.android.sbz.tool.LoadingDialogTool
import com.yjhb.android.sbz.tool.Tool
import com.yjhb.android.sbz.v2.activity.V2InviteFriendActivity
import com.yjhb.android.sbz.v2.adapter.V2FissionRewardAdapter
import com.yjhb.android.sbz.v2.entity.FissionRewardItem
import com.yjhb.android.sbz.v2.entity.UserFissionRewardInfo
import com.yjhb.android.sbz.v2.mvp.presenter.ConfigPresenter
import com.yjhb.android.sbz.v2.mvp.presenter.V2RewardPresenter
import com.yjhb.android.sbz.v2.mvp.view.ConfigView
import com.yjhb.android.sbz.v2.mvp.view.V2RewardView
import com.jojo.android.zxlib.base.BaseFragment
import com.jojo.android.zxlib.base.BaseRecyclerAdapterV2
import com.jojo.android.zxlib.tool.ZxActivityTool
import com.jojo.android.zxlib.tool.ZxToast
import com.jojo.android.zxlib.view.dialog.ILoading
import kotlinx.android.synthetic.main.fragment_fission_reward_v2.*
import kotlinx.android.synthetic.main.fragment_fission_reward_v2.now_make_money_tv
import kotlinx.android.synthetic.main.fragment_money_make_reward_v2.activity_time_tv
import kotlinx.android.synthetic.main.fragment_money_make_reward_v2.emptyRecyclerView
import kotlinx.android.synthetic.main.fragment_money_make_reward_v2.emptyView
import java.util.*


/**
 *  创建者 zx
 *  创建时间 2021/4/1 15:49
 */
class FragmentFissionRewardV2 : BaseFragment<V2RewardPresenter<FissionRewardItem, UserFissionRewardInfo>>(),
        V2RewardView<FissionRewardItem, UserFissionRewardInfo>, ConfigView {


    private var adapterV2: V2FissionRewardAdapter? = null
    private val data = mutableListOf<FissionRewardItem>()
    private var userInfo: UserFissionRewardInfo? = null
    private var loadingView: ILoading? = null

    private val configPresenter = lazy { ConfigPresenter(this) }

    override fun initPresenter(): V2RewardPresenter<FissionRewardItem, UserFissionRewardInfo>? {
        return V2RewardPresenter(this)
    }

    override fun getLayoutId(): Int {
        return R.layout.fragment_fission_reward_v2
    }

    override fun initUI() {
        val calendar = Calendar.getInstance()
        //设置时间
        val time = StringBuilder("活动时间：")
                .append(calendar[Calendar.YEAR])
                .append(".")
                .append(calendar[Calendar.MONTH] + 1)
                .append(".")
                .append("1")
                .append("-")
                .append(calendar[Calendar.YEAR])
                .append(".")
                .append(calendar[Calendar.MONTH] + 1)
                .append(".")
                .append(calendar.getActualMaximum(Calendar.DAY_OF_MONTH))
                .toString()
        activity_time_tv.text = time
        //设置空视图
        emptyView.showLoading()
        emptyRecyclerView.emptyView = emptyView
        activity?.run {
            adapterV2 = V2FissionRewardAdapter(this)
            adapterV2
        }?.let {
            it.data = this.data
        }
        emptyRecyclerView.adapter = this.adapterV2
        loadingView = LoadingDialogTool.createDialog(this)

        configPresenter.value.getSysConfig("FriendStageReward")

    }

    override fun initEvent() {
        emptyView.setOnReloadClickListener {
            presenter.getRankList(4, FissionRewardItem::class.java)
        }
        adapterV2?.setOnItemClick(object : BaseRecyclerAdapterV2.ItemClickListener<FissionRewardItem?> {
            override fun onViewClick(view: View?, msg: String?, index: Int, data: FissionRewardItem?) {
                if (msg == "receive") {
                    data?.let {
                        presenter.getStageReward(it.stageNum)
                    }
                }
            }

            override fun onItemClick(index: Int, data: FissionRewardItem?) {

            }
        })
        now_make_money_tv.setOnClickListener {
            ZxActivityTool.skipActivity(activity, V2InviteFriendActivity::class.java)
        }
    }

    override fun initData() {
        presenter.getUserRankInfo(4, UserFissionRewardInfo::class.java)
    }

    override fun onLoadingList() {
        emptyView.showLoading()
    }

    override fun onLoadingUserInfo() {
        loadingView?.show()
    }

    override fun onListSuccess(period: String, data: List<FissionRewardItem>) {
        this.data.clear()
        this.data.addAll(data)
        userInfo?.friendGetMoneyCount?.let {
            adapterV2?.currNum = it
        }
        userInfo?.currentStage?.let {
            adapterV2?.currentStage = it
        }
        adapterV2?.notifyDataSetChanged()
    }

    override fun onUserInfoSuccess(userInfo: UserFissionRewardInfo) {
        this.userInfo = userInfo
        fission_money_tv.text = Tool.getPrettyNumber(userInfo.currentReward).toString() + "￥"
        fission_num_tv.text = "${userInfo.friendGetMoneyCount}人"
        loadingView?.cancel()
        presenter.getRankList(4, FissionRewardItem::class.java)
    }

    override fun onFailure(type: Int, code: Int, msg: String) {
        when (type) {
            1 -> {
                emptyView.showFail(msg, true)
            }
            else -> {
                loadingView?.cancel()
                ZxToast.showINFOBottomToast(msg)
            }

        }
    }

    override fun onConfig(config: JSONObject?) {
        config?.let {
            reward_title_tv?.text = it.getString("title")
            fission_num_title_tv?.text = it.getString("currentFriendStageReward")
            fission_money_title_tv?.text = it.getString("currentReward")
            banner_tv?.text = it.getString("banner")
            tableTile_tv?.text = it.getString("tableTile")
        }
    }

    override fun onConfig(config: JSONArray?) {

    }

    override fun onFailure(code: Int, msg: String?) {

    }

    override fun receiving() {
        loadingView?.show()
    }

    override fun successReceived() {
        loadingView?.cancel()
        initData()
    }
}